<template>
  <div class='search'>
    <f-form ref='modalForm' 
            :action='api.payment' 
            handlerName='hyHkPaymentSaveHandler' 
            :form-key='this.formItem.paymentId' 
            :file-ids='fileIds'>
      <div style='height: 440px;margin-bottom: -120px'>
        <f-form-item label='回款单位' disabled name='customName' :value='formItem.customName' />
        <f-form-item label='发货金额' disabled name='saleAmount' :value='formItem.saleAmount' />
        <f-form-item label='回款金额' placeholder='请选择回款金额' name='amount' disabled :required='true' :value.sync='formItem.amount' :trigger-change='true'>
          <a-tooltip slot='addonBefore' placement='top' title='点击图标选择银行流水'>
            <a-select default-value='1'>
              <a-select-option value='1' @click='openBankStatementModal'>
                银行流水
              </a-select-option>
              <a-select-option value='2' @click='openHkBalanceModal'>
                回款记录
              </a-select-option>
            </a-select>
          </a-tooltip>
        </f-form-item>
        <f-form-item label='回款日期' type='date' name='amountDate' disabled :required='true' :value='formItem.amountDate' />
        <f-form-item label='收款账户' placeholder='请选择收款账户' disabled name='bankName' :readOnly='true' :required='true' :value.sync='formItem.bankName' :trigger-change='true'>
          <a-tooltip slot='addonBefore' placement='top' title='点击图标选择收款账户'>
            <a-icon type='money-collect' style='cursor:pointer' />
          </a-tooltip>
        </f-form-item>
        <f-form-item label='备注' name='remark' :value='formItem.remark' />
        <f-form-item label='附件' type='upload' name='hkImage' :value='formItem.hkImage' :file-max='3' :busId='this.formItem.orderDetailsId' busType='HyOrderDetails' :realRemove='true' @uploadSuccess='uploadSuccess' />
      </div>
    </f-form>
    <select-bank-statement-modal ref='bankStatementModal' @onSelectChange='onSelectBankStatementModalChange'></select-bank-statement-modal>
    <select-hk-balance-modal ref='hkBalanceModal' @onSelectChange='onSelectHkStatementModalChange'></select-hk-balance-modal>
  </div>
</template>
<script>
import api from "../../../js/api"
import SelectBankStatementModal from "@/views/module/haiying/components/select_bank_statement_modal"
import SelectHkBalanceModal from "@/views/module/haiying/components/select_hk_balance_modal"


export default {
  name: "ReturnForm",
  props: {},
  data() {
    return {
      refs: this.$refs,
      api: Object.assign(api, this.api),
      loading: false,
      visible: false,
      formItem: this.getFormItem(),
      queryParam: {},
      removeIds: [],
      fileIds: []
    }
  },
  watch: {},
  methods: {
    getFormItem() {
      return {
        saleNum: 0,
        saleAmount: 0,
        orderId: null,
        returnDate: null,
        bankId: null,
        remark: null,
        settleType: 1,
        hkImage: []
      }
    },
    setData(record) {
      this.$refs.modalForm.resetFields()
      this.formItem = this.getFormItem()
      if (record.selectIds) {
        this.selectIds = record.selectIds
        this.getReturnInfo()
      }
      this.formItem.hkImage = []
      this.visible = true
    },

    /**
     * 获取回款信息
     */
    getReturnInfo() {
      this.http.get(this, {
        url: this.api.payment.getReturnInfo,
        params: {
          selectIds: this.selectIds
        },
        success: (me, data) => {
          this.$nextTick(() => {
            this.formItem.customId = data.customId
            this.formItem.customName = data.customName
            this.formItem.customCode = data.customCode
            this.formItem.saleAmount = data.saleAmount
            this.$forceUpdate()
          })
        }
      })
    },
    /**
     * 选择收款银行
     */
    openBankModal() {
      this.$refs["bankModal"].open()
    },
    /**
     * 打开银行流水
     */
    openBankStatementModal() {
      this.$refs["bankStatementModal"].open({
        customId: this.formItem.customId,
        customCode: this.formItem.customCode
      })
    },
    /**
     * 打开回款记录
     */
    openHkBalanceModal() {
      this.$refs["hkBalanceModal"].open({
        customId: this.formItem.customId,
        customCode: this.formItem.customCode
      })
    },
    /**
     * 银行流水选择回调
     */
    onSelectBankStatementModalChange(selectedRowKeys, selectedRows) {
      this.$refs.modalForm.getForm().setFieldsValue({ "amount": selectedRows[0].amount + "" })
      this.formItem.amountDate = selectedRows[0].amountDate
      this.formItem.amount = selectedRows[0].amount + ""
      this.formItem.surplusAmount = selectedRows[0].amount + ""
      this.formItem.bankName = selectedRows[0].takeBankName
      this.formItem.bankId = selectedRows[0].takeBankId
      this.formItem.bankNo = selectedRows[0].takeBankNo
      this.formItem.payUserName = selectedRows[0].customName
      this.formItem.sendArea = selectedRows[0].sendArea
      this.formItem.payBankNo = selectedRows[0].bankNo
      this.formItem.payBankName = selectedRows[0].bankName
      this.formItem.bankStatementId = selectedRows[0].bankStatementId
      this.$forceUpdate()
    },
    /**
     * 回款选择回调
     */
    onSelectHkStatementModalChange(selectedRowKeys, selectedRows) {
      this.$refs.modalForm.getForm().setFieldsValue({ "amount": selectedRows[0].amount + "" })
      this.formItem.amountDate = selectedRows[0].amountDate
      this.formItem.amount = selectedRows[0].amount + ""
      this.formItem.surplusAmount = selectedRows[0].surplusAmount + ""
      this.formItem.bankName = selectedRows[0].bankName
      this.formItem.bankId = selectedRows[0].bankId
      this.formItem.bankNo = selectedRows[0].bankNo
      this.formItem.payUserName = selectedRows[0].payUserName
      this.formItem.sendArea = selectedRows[0].sendArea
      this.formItem.payBankNo = selectedRows[0].payBankNo
      this.formItem.payBankName = selectedRows[0].payBankName
      this.formItem.bankStatementId = selectedRows[0].bankStatementId
      this.formItem.parentId = selectedRows[0].paymentId
      this.$forceUpdate()
    },
    /**
     * 文件上传回调
     */
    uploadSuccess(fileUrl, files, fileIds, removeIds) {
      this.fileIds = fileIds
      this.removeIds.push(...removeIds)
    },
    handleSubmit(callback) {
      this.$refs.modalForm.submit({
        selectIds: this.selectIds,
        bankId: this.formItem.bankId,
        bankStatementId: this.formItem.bankStatementId,
        parentId: this.formItem.parentId,
        customId: this.formItem.customId,
        amount: this.formItem.amount,
        surplusAmount: this.formItem.surplusAmount,
        payBankId: this.formItem.payBankId,
        payUserName: this.formItem.payUserName,
        amountDate: this.formItem.amountDate,
        sendAreaId: this.formItem.sendAreaId,
        sendArea: this.formItem.sendArea,
        payBankNo: this.formItem.payBankNo,
        saleAmount: this.formItem.saleAmount
      }, callback)
    }
  },
  created() {
  },
  components: { SelectHkBalanceModal, SelectBankStatementModal }
}
</script>

<style scoped lang='less' rel='stylesheet/less'>

</style>
